package weaver.interfaces.dfhj.duss.action;

import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.dfhj.duss.util.CalUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-10-28
 * @Description: 工程采购/施工合同审批流程
 * @ModifyLog:
 **/
/*
 *@Description: 工程采购/施工合同审批流程提交后生成合同编号
 *@Param:
 *@return:
 *@Author: Dusongsong
 *@Date: 2020/12/10
 */
public class WeaHtspSerialNumAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        baseBean.writeLog(">>>>>WeaHtspSerialNumAction>>>>>" + requestid);
        try {
            rs.executeQuery("select htbhdxwb,htlx1,lsh,sfrdxm from " + tablename + " where requestid=?", requestid);
            rs.next();
            //所属项目
            String htbhdxwb = Util.null2String(rs.getString("htbhdxwb"));
            //合同类型
            String htlx1 = Util.null2String(rs.getString("htlx1"));
            //流水号
            String lsh = Util.null2String(rs.getString("lsh"));
            //是否RD项目
            String sfrdxm = Util.null2String(rs.getString("sfrdxm"));
            //防止有退回流程的情况，重复生成流水号
            if ("".equals(lsh)) {
                String maxlsh = getMaxLsh(tablename, htbhdxwb);
                maxlsh = "".equals(maxlsh) ? "0001" : maxlsh;
                String newlsh = CalUtil.autoGenericCode(maxlsh, 4);
                String htlx = getXmbh(htlx1);
                String htbh = htbhdxwb + "-" + htlx + newlsh;
                htbh = "0".equals(sfrdxm)?htbh + "-RD-1":htbh + "-1";
                String sql = "update " + tablename + " set lsh=?,htbh=? where requestid=?";
                rs.executeUpdate(sql, newlsh, htbh, requestid);
                baseBean.writeLog(">>>>>" + sql + "," + newlsh + "," + htbh + "," + requestid);

            }

            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog("异常参考:" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口异常！");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

    public static String getXmbh(String id) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select dm from uf_htlx where dm in ('SBC','CLC','YSC','GCF','ABC') and id=?", id);
        rs.next();
        String dm = Util.null2String(rs.getString("dm"));
        return dm;
    }

    public static String getMaxLsh(String tablename, String htbhdxwb) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select max(lsh) from " + tablename + " where htbhdxwb=?", htbhdxwb);
        rs.next();
        String lsh = Util.null2String(rs.getString(1));
        return lsh;
    }

}
